package com.dmap.analysispoint.service.impl;

import com.dmap.analysispoint.bo.DateAndNumBo;
import com.dmap.analysispoint.bo.NameAndValueBo;
import com.dmap.analysispoint.controller.background.param.CountParam;
import com.dmap.analysispoint.dao.BurialPointAddEquipmentMapper;
import com.dmap.analysispoint.dao.BurialPointDayEquipmentMapper;
import com.dmap.analysispoint.dao.BurialPointHourEquipmentMapper;
import com.dmap.analysispoint.po.BurialPointAddEquipmentExample;
import com.dmap.analysispoint.po.BurialPointDayEquipmentExample;
import com.dmap.analysispoint.po.BurialPointHourEquipmentExample;
import com.dmap.analysispoint.service.EquipmentInfoAnalysisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * \* @author:zhouhao
 * \* Date:11/05/2018
 * \* Description:
 * \*
 */
@Service
public class EquipmentInfoAnalysisServiceImpl implements EquipmentInfoAnalysisService {

    @Resource
    private BurialPointAddEquipmentMapper burialPointAddEquipmentMapper;

    @Resource
    private BurialPointHourEquipmentMapper burialPointHourEquipmentMapper;

    @Resource
    private BurialPointDayEquipmentMapper burialPointDayEquipmentMapper;

    /**
     *统计新增设备总数
     */
    @Override
    public Integer countEquipmentNum(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointAddEquipmentExample example = this.getParamExample(param);
        return burialPointAddEquipmentMapper.countEquipmentByExample(example);
    }

    /**
     *去重统计查询设备固件各个版本新增设备数
     */
    @Override
    public List<NameAndValueBo> countFirmwareVersionAddPhoneNum(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointAddEquipmentExample example = this.getParamExample(param);
        return burialPointAddEquipmentMapper.countFirmwareVersionByExample(example);
    }

    /**
     *按小时分组统计新增设备数
     */
    @Override
    public List<DateAndNumBo> countAddEquipmentNumList(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointAddEquipmentExample example = this.getParamExample(param);
        return burialPointAddEquipmentMapper.countEquipmentListByExample(example);
    }

    /**
     *BurialPointAddEquipment表查询时对查询条件进行处理
     */
    private BurialPointAddEquipmentExample getParamExample(CountParam param) {
        BurialPointAddEquipmentExample example = new BurialPointAddEquipmentExample();
        BurialPointAddEquipmentExample.Criteria criteria = example.createCriteria();
        if (param.getCompanyId() != null){
            criteria.andCompanyIdEqualTo(param.getCompanyId());
        }
        if (!StringUtils.isBlank(param.getProjectId())){
            criteria.andProjectIdEqualTo(param.getProjectId());
        }
        if (param.getPlaceId() != null){
            criteria.andPlaceIdEqualTo(param.getPlaceId());
        }
        if (!StringUtils.isBlank(param.getAppVersion())) {
            criteria.andAppVersionEqualTo(param.getAppVersion());
        }
        if (!StringUtils.isBlank(param.getFirmwareId())) {
            criteria.andFirmwareIdEqualTo(param.getFirmwareId());
        }
        if (!StringUtils.isBlank(param.getFirmwareVersion())) {
            criteria.andFirmwareVersionEqualTo(param.getFirmwareVersion());
        }
        if (param.getStartTime() != null && param.getEndTime() != null && param.getEndTime() != 0) {
            criteria.andCreateTimeBetween(param.getStartTime(), param.getEndTime());
        }
        return example;
    }

    /**
     *按小时分组统计活跃设备数
     */
    @Override
    public List<DateAndNumBo> countActiveEquipmentList(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointHourEquipmentExample example = new BurialPointHourEquipmentExample();
        BurialPointHourEquipmentExample.Criteria criteria = example.createCriteria();
        if (param.getCompanyId() != null){
            criteria.andCompanyIdEqualTo(param.getCompanyId());
        }
        if (!StringUtils.isBlank(param.getProjectId())){
            criteria.andProjectIdEqualTo(param.getProjectId());
        }
        if (param.getPlaceId() != null){
            criteria.andPlaceIdEqualTo(param.getPlaceId());
        }
        if (!StringUtils.isBlank(param.getAppVersion())) {
            criteria.andAppVersionEqualTo(param.getAppVersion());
        }
        if (!StringUtils.isBlank(param.getFirmwareId())) {
            criteria.andFirmwareIdEqualTo(param.getFirmwareId());
        }
        if (!StringUtils.isBlank(param.getFirmwareVersion())) {
            criteria.andFirmwareVersionEqualTo(param.getFirmwareVersion());
        }
        if (param.getStartTime() != null && param.getEndTime() != null && param.getEndTime() != 0) {
            criteria.andLoginTimeBetween(param.getStartTime(), param.getEndTime());
        }
        return burialPointHourEquipmentMapper.countActiveEquipmentListByExample(example);
    }


    /**
     *统计活跃设备数（跨天）
     */
    @Override
    public Integer countActiveEquipmentNumByDay(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointDayEquipmentExample example = this.getDayParamExample(param);
        return burialPointDayEquipmentMapper.countEquipmentByExample(example);
    }

    /**
     *BurialPointDayEquipment表查询时对查询条件进行处理
     */
    private BurialPointDayEquipmentExample getDayParamExample(CountParam param) {
        BurialPointDayEquipmentExample example = new BurialPointDayEquipmentExample();
        BurialPointDayEquipmentExample.Criteria criteria = example.createCriteria();
        if (param.getCompanyId() != null){
            criteria.andCompanyIdEqualTo(param.getCompanyId());
        }
        if (!StringUtils.isBlank(param.getProjectId())){
            criteria.andProjectIdEqualTo(param.getProjectId());
        }
        if (param.getPlaceId() != null){
            criteria.andPlaceIdEqualTo(param.getPlaceId());
        }
        if (!StringUtils.isBlank(param.getAppVersion())) {
            criteria.andAppVersionEqualTo(param.getAppVersion());
        }
        if (!StringUtils.isBlank(param.getFirmwareId())) {
            criteria.andFirmwareIdEqualTo(param.getFirmwareId());
        }
        if (!StringUtils.isBlank(param.getFirmwareVersion())) {
            criteria.andFirmwareVersionEqualTo(param.getFirmwareVersion());
        }
        if (param.getStartTime() != null && param.getEndTime() != null && param.getEndTime() != 0) {
            criteria.andLoginTimeBetween(param.getStartTime(), param.getEndTime());
        }
        return example;
    }
}
